package com.gaohe.utils;

import com.aliyun.dypnsapi20170525.Client;
import com.aliyun.dypnsapi20170525.models.SendSmsVerifyCodeRequest;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

public class SendCodeUtil {
    //accessKeyId
    private static  String accessKeyId= "LTAI5tKygkn15e7juGCuv33C";
    //accessKeySecret
    private static  String accessKeySecret= "5W0NT9hexUemCeGVe4LlkQX17HzczQ";
    //测试签名 可更换
    private static  String signName= "速通互联验证码";
    //测试模板编号 可更换
    private static  String templateCode= "100001";
    public static Client createClient() throws Exception {
        Config config = new Config()
                // 设置 ALIBABA_CLOUD_ACCESS_KEY_ID
                .setAccessKeyId(accessKeyId)
                // ALIBABA_CLOUD_ACCESS_KEY_SECRET 读取
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dypnsapi.aliyuncs.com";
        return new Client(config);
    }
    public static void sendCode(String phone,String code) throws Exception {
        Client client = SendCodeUtil.createClient();
        //生成验证码
        SendSmsVerifyCodeRequest sendSmsVerifyCodeRequest =
                new SendSmsVerifyCodeRequest()
                        //接受验证码的手机号
                        .setPhoneNumber(phone)
                        // 测试签名
                        .setSignName(signName)
                        // 测试模板
                        .setTemplateCode(templateCode)
                        //设置验证码 + 时长
                        .setTemplateParam("{\"code\":\"" + code + "\",\"min\":\"5\"}");
        RuntimeOptions runtime = new RuntimeOptions();
        try {
            //发送验证码
            client.sendSmsVerifyCodeWithOptions(sendSmsVerifyCodeRequest, runtime);
        } catch (Exception error) {
            System.out.println("发送失败: " + error.getMessage());
        }
    }
}
